
# event for main menu
from EventDefine import event_MAINMENU,event_SINGLEPLAYERGAME,event_NETWORKGAME,event_GAMEMGRFSM
from EventDefine import event_CONFIGURE,event_MENU_BACKTOPREVIOUS,event_CREATOR
# event for game fsm
from Shell.data.GameEventDefine import event_GAME_GAMELOAD
# joystick initialize
from Cube.controller.JoystickControl import JoyStickInit
# gui
from Shell.gui.MainMenuFrame import MainMenuFrame
from SingleGameMenuFSM import SingleGameMenuFSM
from GameLobbyFSM import GameLobbyFSM
from Shell.gui.ConfigureMenuFrame import ConfigureMenuFrame
from Shell.gui.CreatorFrame import CreatorFrame
# game
from Cube.map import QMapMgrFSM
# fsm
from direct.fsm import FSM
from Cube.gui.PreludeFinale import FSMExtend as PreludeFinaleFSM
# screen shot
from Cube.utility.RoutineEventHandle import ScreenShotEventHandle

class MainMenuFSM(FSM.FSM, PreludeFinaleFSM):
    def __init__(self):
        FSM.FSM.__init__(self, name = 'main menu')

        base.disableMouse()       #Disable default mouse-based camera control

        self.ScreenShotHandle = ScreenShotEventHandle('f12')  # event handle of screen shot
        JoyStickInit()  # initial joystick

        self.demand('MainMenu')

    def enterMainMenu(self):
        self.MainMenuFrame = MainMenuFrame()
        self.acceptOnce(event_SINGLEPLAYERGAME, self.demand, ["SinglePlayerGame"])
        self.acceptOnce(event_NETWORKGAME, self.demand, ["NetworkGame"])
        self.acceptOnce(event_CONFIGURE, self.demand, ["Configure"])
        self.acceptOnce(event_CREATOR, self.demand, ["Creator"])

    def exitMainMenu(self):
        self.ignoreAll()
        self.MainMenuFrame.destroy()

    def filterMainMenu(self, request, args):
        self.NextStateSet(request)   # transfer to wait state
        return self.WaitFinaleState

    def enterSinglePlayerGame(self):
        self.acceptOnce(event_MAINMENU, self.demand, ["MainMenu"])
        self.singleGameMenu = SingleGameMenuFSM()

    def exitSinglePlayerGame(self):
        self.singleGameMenu = None
        self.ignoreAll()

    def enterNetworkGame(self):
        self.acceptOnce(event_MAINMENU, self.demand, ["MainMenu"])
        self.acceptOnce(event_GAMEMGRFSM, self.demand, ["Game"])
        self.gameLobby = GameLobbyFSM()

    def exitNetworkGame(self):
        self.gameLobby = None
        self.ignoreAll()

    def enterConfigure(self):
        self.ConfigureMenuFrame = ConfigureMenuFrame()
        self.acceptOnce(event_MENU_BACKTOPREVIOUS, self.demand, ["MainMenu"])

    def exitConfigure(self):
        self.ignoreAll()
        self.ConfigureMenuFrame.destroy()

    def filterConfigure(self, request, args):
        self.NextStateSet(request)   # transfer to wait state
        return self.WaitFinaleState

    def enterCreator(self):
        self.CreatorFrame = CreatorFrame()
        self.acceptOnce(event_MAINMENU, self.demand, ["MainMenu"])

    def exitCreator(self):
        self.ignoreAll()
        self.CreatorFrame.destroy()

    def filterCreator(self, request, args):
        self.NextStateSet(request)   # transfer to wait state
        return self.WaitFinaleState

    def enterGame(self):
        self.acceptOnce(event_MAINMENU, self.demand, ["MainMenu"])
        self.gameFSM = QMapMgrFSM()

    def exitGame(self):
        self.gameFSM = None
        self.ignoreAll()

    # screen shot handle ---------------------------------------------
    def ScreenShotHandle(self):
        base.screenshot('ss')
